#!/bin/bash

# Return value
Ture=0
False=1

# Path record
src_path=`pwd`
script_path=$( cd $(dirname $BASH_SOURCE)  && pwd) #Current script path, the reset script must be stored in the same directory.

# File
WLAN_FW="WLAN.HL.3.2.4"
find_path=".find_path.txt"

# Show how to use this shell
function usage()
{
    printf "usage: bash FILE.SH \033[33m[-QCOM|-MTK|-HW|-M328|-M329]\033[0m \n"
    printf "                            \033[32m[-wifi-ap][-wifi-cp] ...\033[0m \n"
    printf "                            \033[32m[-bt-ap][-bt-cp] ...\033[0m \n"
    printf "optional arguments:\n"
    printf "\033[33m  -HW, -QCOM, -MTK        This is the platform for the specified update\033[0m\n"
    printf "\033[32m  -h, --help              search for help information\033[0m\n"
    printf "\033[32m  -wifi-ap                update wifi bin to ap\033[0m\n"
    printf "\033[32m  -wifi-cp                update wifi bin to modem\033[0m\n"
    printf "\033[32m  -bt-ap                  update bt bin to ap\033[0m\n"
    printf "\033[32m  -bt-cp                  update bt bin to modem\033[0m\n"
    exit $Ture
}

# HarmonyOS update the bin file
function HarmonyOS_update_bin()
{
    while [ $_ArrayNum -gt 0 ]
    do
        case ${_ArrayName[$_ArrayNum]} in
        -wifi-ap)
            printf "\033[32mPrepare the bdwlan.bin bdwlan.txt file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "wifi bin files path: " wifi_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (ap): " update_branch_path
            update_branch_path=${update_branch_path}vendor/qcom/opensource/wlan/qcacld-3.0/
            cd ${update_branch_path}
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                printf "\033[32m[------ $script_path/reset.sh: ${update_branch_path} ------]\033[0m \n"
                $script_path/reset.sh .
                printf "\033[32m[------ copy bdwlan file to: ${update_branch_path} ------]\033[0m \n"
                rm -rf firmware_bin_hw/bdwlan.bin firmware_bin_hw/jln_al00_bdwlan.bin
                cp $wifi_bin_files_path/bdwlan.bin firmware_bin_hw/bdwlan.bin
                cp $wifi_bin_files_path/bdwlan.bin firmware_bin_hw/jln_al00_bdwlan.bin
                mkdir firmware_bin_hw/Julia
                cp $wifi_bin_files_path/bdwlan.txt firmware_bin_hw/Julia/bdwlan.txt
                printf "\033[31m[>>>>>> please repo upload this git repository: ${update_branch_path} <<<<<<]\033[0m \n"
            fi
        ;;
        -wifi-cp)
            printf "\033[32mPrepare the bdwlan.bin bdwlan.txt file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "wifi bin files path: " wifi_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (cp): " update_branch_path
            update_branch_path=${update_branch_path}WLAN.HL.3.2.4/
            cd ${update_branch_path}
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                printf "\033[32m[------ $script_path/reset.sh: ${update_branch_path} ------]\033[0m \n"
                $script_path/reset.sh .
                printf "\033[32m[------ copy bdwlan file to: ${update_branch_path} ------]\033[0m \n"
                rm -rf wlan_proc/wlan/halphy_tools/host/bdfUtil/qca61x0/bdf/bdwlan.bin
                cp $wifi_bin_files_path/bdwlan.bin wlan_proc/wlan/halphy_tools/host/bdfUtil/qca61x0/bdf/bdwlan.bin
                printf "\033[31m[>>>>>> please repo upload this git repository: ${update_branch_path} <<<<<<]\033[0m \n"
            fi
        ;;
        -bt-ap)
            printf "\033[32mPrepare the cmnv13.bin cmnv13.nvm jln_bt-new.nvm file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "bt bin files path: " bt_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (ap): " update_branch_path
            update_branch_path=${update_branch_path}vendor/huawei/extra_qcom/connectivity/
            cd ${update_branch_path}
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                printf "\033[32m[------ $script_path/reset.sh: ${update_branch_path} ------]\033[0m \n"
                $script_path/reset.sh .
                printf "\033[32m[------ copy bdwlan file to: ${update_branch_path} ------]\033[0m \n"
                rm -rf bt/cust_nv_bin/bengal/jln_cmnv13.bin bt/cust_nv_bin/bengal/cmnv13.nvm bt/cust_nv_bin/bengal/jln_bt-new.nvm
                cp $bt_bin_files_path/cmnv13.bin bt/cust_nv_bin/bengal/jln_cmnv13.bin
                cp $bt_bin_files_path/cmnv13.nvm bt/cust_nv_bin/bengal/cmnv13.nvm
                cp $bt_bin_files_path/jln_bt-new.nvm bt/cust_nv_bin/bengal/jln_bt-new.nvm
                printf "\033[31m[>>>>>> please repo upload this git repository: ${update_branch_path} <<<<<<]\033[0m \n"
            fi
        ;;
        -bt-cp)
            printf "\033[32mPrepare the cmnv13.bin cmbtfw13.tlv cmbtfw13.ver file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "bt bin files path: " bt_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (cp): " update_branch_path
            update_branch_path=${update_branch_path}BTFM.CMC.1.3.0/
            cd ${update_branch_path}
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                printf "\033[32m[------ $script_path/reset.sh: ${update_branch_path} ------]\033[0m \n"
                $script_path/reset.sh .
                printf "\033[32m[------ copy bdwlan file to: ${update_branch_path} ------]\033[0m \n"
                rm -rf 	btfm_proc/bt/build/ms/bin/WCN3950/cmbtfw13.tlv btfm_proc/bt/build/ms/bin/WCN3950/cmbtfw13.ver btfm_proc/bt/build/ms/bin/WCN3950/cmnv13.bin
                cp $bt_bin_files_path/cmnv13.bin btfm_proc/bt/build/ms/bin/WCN3950/cmnv13.bin
                cp $bt_bin_files_path/cmbtfw13.tlv btfm_proc/bt/build/ms/bin/WCN3950/cmbtfw13.tlv
                cp $bt_bin_files_path/cmbtfw13.ver btfm_proc/bt/build/ms/bin/WCN3950/cmbtfw13.ver
                printf "\033[31m[>>>>>> please repo upload this git repository: ${update_branch_path} <<<<<<]\033[0m \n"
            fi
        ;;
        *)
            printf "Try 'bash [FILE.SH] --help' for more information.\n"
            exit $False
        ;;
        esac
        shift

        let _ArrayNum--
    done
}

# QCOM update the bin file
# Instructions: QCOM_update_bin bdwlan_devon_ipa # bdwlan_devon_ipa.bin & bdwlan_devon_ipa.txt
function QCOM_update_bin()
{
    while [ $_ArrayNum -gt 0 ]
    do
        case ${_ArrayName[$_ArrayNum]} in
        -wifi-ap)
        ;;
        -wifi-cp)
            printf "\033[32mPrepare the bdwlan.bin bdwlan.txt file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "wifi bin files path: " wifi_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (all): " update_branch_path
            cd ${update_branch_path}
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                # Search bdwlan.bin go to modem
                printf "\033[32m[INFO]\033[0m Search bdwlan.bin go to modem!\n"
                path_num=`find . -name bdwlan.bin | grep ${WLAN_FW} | wc -l`
                if [[ ${path_num} -gt 1 ]];then
                    printf "\033[33m[WARNING]\033[0m modem git repository is more than 1!\n"
                    return ${False}
                elif [[ ${path_num} -eq 1 ]];then
                    printf "\033[32m[INFO]\033[0m modem git repository is equal to 1!\n"
                    path=`find . -name bdwlan.bin | grep ${WLAN_FW}`
                else 
                    printf "\033[33m[WARNING]\033[0m modem git repository is less than 1!\n"
                    return ${False}
                fi
                modem_path=`echo ${path%${WLAN_FW}*}`
                printf "\033[32m[INFO]\033[0m modem path is ${modem_path}\n"
                cd ${modem_path}
                
                # Find all repository paths
                printf "\033[32m[INFO]\033[0m Start finding local repositories ...\n"
                find . -type d  -name .git 2>/dev/null > ${script_path}/${find_path} 
                sed -i "s/.git//g" ${script_path}/${find_path} # remove .git in find_path
                
                path_num=`cat ${script_path}/${find_path} | grep ${WLAN_FW} | wc -l`
                if [[ ${path_num} -gt 1 ]];then
                    printf "\033[33m[WARNING]\033[0m bdwlan git repository is more than 1!\n"
                    return ${False}
                elif [[ ${path_num} -eq 1 ]];then
                    printf "\033[32m[INFO]\033[0m bdwlan git repository is equal to 1!\n"
                    cd `cat ${script_path}/${find_path} | grep ${WLAN_FW}`
                    rm -rf ${script_path}/${find_path}
                else 
                    printf "\033[32m[INFO]\033[0m bdwlan git repository is less than 1!\n"
                fi
                
                printf "\033[32m[------ $script_path/reset.sh: `pwd` ------]\033[0m \n"
                $script_path/reset.sh .
                if [ $? -ne 0 ];then
                    printf "\033[33m[WARNING]\033[0m sync is False ...\n"
                    return ${False}
                fi
                printf "\033[32m[------ copy bdwlan file to: `pwd` ------]\033[0m \n"
                path=`find . -name bdwlan.bin`
                file_path=`echo ${path%bdwlan.bin*}`
                printf "\033[32m[INFO]\033[0m file path is ${file_path}\n"
                rm -rf ${file_path}${1}.bin ${file_path}${1}.txt
                cp $wifi_bin_files_path/bdwlan.bin ${file_path}${1}.bin
                cp $wifi_bin_files_path/bdwlan.txt ${file_path}${1}.txt
                printf "\033[31m[>>>>>> please repo upload this git repository: `pwd` <<<<<<]\033[0m \n"
            fi
        ;;
        -bt-ap)
        ;;
        -bt-cp)
        ;;
        *)
            printf "Try 'bash [FILE.SH] --help' for more information.\n"
            exit $False
        ;;
        esac
        shift

        let _ArrayNum--
    done
}

# MTK update the bin file
# Instructions: MTK_update_bin k6855v1_64 # project name
function MTK_update_bin()
{
    while [ $_ArrayNum -gt 0 ]
    do
        case ${_ArrayName[$_ArrayNum]} in
        -wifi-ap)
            printf "\033[32mPrepare the CFG_WIFI_Default_new.h file.\033[0m \n"
            printf "\033[33m[InPut] \033[0m"
            read -e -p "wifi bin files path: " wifi_bin_files_path
            printf "\033[33m[InPut] \033[0m"
            read -e -p "updated git repository path (all): " update_branch_path
            cd ${update_branch_path}/vendor/mediatek/proprietary/custom/
            if [ $? -ne 0 ];then
                # printf "$?\n"
                printf "\033[31m[****** Error Path: ${update_branch_path} ******]\033[0m \n"
                exit $False
            else
                # reset git repository
                printf "\033[32m[------ $script_path/reset.sh: `pwd` ------]\033[0m \n"
                $script_path/reset.sh .
                if [ $? -ne 0 ];then
                    printf "\033[33m[WARNING]\033[0m sync is False ...\n"
                    return ${False}
                fi
                
                printf "\033[32m[------ copy bdwlan file to: `pwd` ------]\033[0m \n"
                # Search CFG_WIFI_Default.h
                printf "\033[32m[INFO]\033[0m Search CFG_WIFI_Default.h!\n"
                path_num=`find . -name CFG_WIFI_Default.h | grep ${1} | wc -l`
                if [[ ${path_num} -gt 1 ]];then
                    printf "\033[33m[WARNING]\033[0m CFG_WIFI_Default.h is more than 1!\n"
                    return ${False}
                elif [[ ${path_num} -eq 1 ]];then
                    printf "\033[32m[INFO]\033[0m CFG_WIFI_Default.h is equal to 1!\n"
                    path=`find . -name CFG_WIFI_Default.h | grep ${1}`
                else 
                    printf "\033[33m[WARNING]\033[0m CFG_WIFI_Default.h is less than 1!\n"
                    return ${False}
                fi
                printf "\033[32m[INFO]\033[0m file path is ${path}\n"

                rm -rf ${path}
                cp ${wifi_bin_files_path}CFG_WIFI_Default_new.h ${path}
                printf "\033[31m[>>>>>> please repo upload this git repository: `pwd` <<<<<<]\033[0m \n"
            fi
        ;;
        -wifi-cp)
        ;;
        -bt-ap)
        ;;
        -bt-cp)
        ;;
        *)
            printf "Try 'bash [FILE.SH] --help' for more information.\n"
            exit $False
        ;;
        esac
        shift

        let _ArrayNum--
    done
}

# Creating a Dynamic Array
_ArrayNum=0
while [ $# -gt 0 ]
do
    _ArrayName[${_ArrayNum}]=$1
    let _ArrayNum++
    shift
done
let _ArrayNum--

# Check if the parameters are correct
if [[ ${_ArrayName[0]} != "-h" ]] && [[ ${_ArrayName[0]} != "--help" ]] && [[ $_ArrayNum < 1  ]];then
    printf "Try 'bash [FILE.SH] --help' for more information.\n"
    exit $False
fi

# Judgment platform
case ${_ArrayName[0]} in
    -h|--help)
        usage
    ;;
    -QCOM)
        printf "\033[33m[ Now updated is the Qualcomm. ]\033[0m\n"
        QCOM_update_bin bdwlan # bdwlan.bin & bdwlan.txt
    ;;
    -MTK)
        printf "\033[33m[ Now updated is the MediaTek. ]\033[0m\n"
    ;;
    -HW)
        printf "\033[33m[ Now updated is the HarmonyOS. ]\033[0m\n"
        HarmonyOS_update_bin
    ;;
    -M328)
        printf "\033[33m[ Now updated is the M328(DEVON 4G). ]\033[0m\n"
        QCOM_update_bin bdwlan_devon_ipa # bdwlan_devon_ipa.bin & bdwlan_devon_ipa.txt
    ;;
    -M329)
        printf "\033[33m[ Now updated is the M329(DEVON 5G). ]\033[0m\n"
        MTK_update_bin k6855v1_64 # project name
    ;;
    *)
        printf "Try 'bash [FILE.SH] --help' for more information.\n"
        exit $False
    ;;
esac
